package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacGermMorphology;
import com.ysd.lis.entity.bac.BacGermMorphologyType;
import com.ysd.lis.entity.bac.BacMorphologyType;
import com.ysd.lis.mapper.bac.BacGermMorphologyMapper;
import com.ysd.lis.mapper.bac.BacGermMorphologyTypeMapper;
import com.ysd.lis.mapper.bac.BacMorphologyTypeMapper;
import com.ysd.lis.request.bac.MorphologyTypeParam;
import com.ysd.lis.service.bac.BacGermMorphologyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菌落形态 服务实现类
 * </p>
 *
 * @author wanghao
 * @since 2024-04-07
 */
@Service
public class BacGermMorphologyServiceImpl extends ServiceImpl<BacGermMorphologyMapper, BacGermMorphology> implements BacGermMorphologyService {

    @Autowired
    BacGermMorphologyMapper bacGermMorphologyMapper;

    @Autowired
    BacGermMorphologyTypeMapper bacGermMorphologyTypeMapper;

    @Autowired
    BacMorphologyTypeMapper bacMorphologyTypeMapper;

    @Override
    public Result findPageList(MorphologyTypeParam morphologyTypeParam) {
        LambdaQueryWrapper<BacGermMorphology> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //参数
        if (!StringUtils.isEmpty(morphologyTypeParam.getSearchValue())) {
            lambdaQueryWrapper.and(wrapper ->
                    wrapper.like(BacGermMorphology::getCode, morphologyTypeParam.getSearchValue())
                            .or().like(BacGermMorphology::getName, morphologyTypeParam.getSearchValue()));
        }
        lambdaQueryWrapper.eq(BacGermMorphology::getDelFlag, 0);
        lambdaQueryWrapper.orderByAsc(BacGermMorphology::getSeq);
        List<BacGermMorphology> bacGermMorphologies = bacGermMorphologyMapper.selectList(lambdaQueryWrapper);
        return Result.succ(bacGermMorphologies);
    }

    @Override
    public Result findListByMorphologyId(String id) {

        if (StringUtils.isEmpty(id)) {
            return Result.succ(new ArrayList<>());
        }
        MPJLambdaWrapper<BacGermMorphologyType> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(BacGermMorphologyType.class);
        lambdaQueryWrapper.leftJoin(BacMorphologyType.class, BacMorphologyType::getMorphologyTypeId, BacGermMorphologyType::getId);
        lambdaQueryWrapper.eq(BacMorphologyType::getMorphologyId, id);
        lambdaQueryWrapper.eq(BacGermMorphologyType::getDelFlag, 0);
        List<BacGermMorphologyType> bacGermMorphologyTypes = bacGermMorphologyTypeMapper.selectList(lambdaQueryWrapper);
        return Result.succ(bacGermMorphologyTypes);
    }

    @Override
    public Result saveMorphologys(BacMorphologyType bacMorphologyType) {
        LambdaQueryWrapper<BacMorphologyType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BacMorphologyType::getMorphologyId, bacMorphologyType.getMorphologyId());
        lambdaQueryWrapper.eq(BacMorphologyType::getMorphologyType, bacMorphologyType.getMorphologyType());
        bacMorphologyTypeMapper.delete(lambdaQueryWrapper);
        if (StringUtils.isNotBlank(bacMorphologyType.getMorphologyTypeId())) {
            bacMorphologyTypeMapper.insert(bacMorphologyType);
        }
        return Result.succ("保存成功!");
    }
}
